/**
 * commit detail 源文件按钮
 */
import React from 'react';
import PropTypes from 'prop-types';
import { Balloon, Button } from '@teamix/ui';
import track from 'utils/track';
import { navigate } from 'utils/router';
import './index.scss';
import { YunxiaoIcon } from '@alife/devops-ui';
import intl from '../../locale';

const { Tooltip } = Balloon;

export default class Btn extends React.Component {
  static propTypes = {
    commitInfo: PropTypes.object,
    pathResource: PropTypes.object,
    resourcePath: PropTypes.string,
    trackPage: PropTypes.string,
    onClick: PropTypes.func,
    inCommitDetail: PropTypes.bool,
  };

  static defaultProps = {
    commitInfo: {},
    pathResource: {},
    resourcePath: '',
    trackPage: '',
    inCommitDetail: true,
  };

  render() {
    const { commitInfo, resourcePath, pathResource, onClick, inCommitDetail, trackPage } =
      this.props;

    let trigger = null;
    if (inCommitDetail) {
      trigger = (
        <Button
          className="src-btn"
          onClick={() => {
            track({
              action: 'view_commit_source_file',
              page: trackPage,
            });

            navigate(
              !resourcePath
                ? `/${pathResource.fullPath}/tree/${commitInfo.id}`
                : `/${pathResource.fullPath}/tree/${commitInfo.id}/${resourcePath}`,
              { state: { forceUpdate: true } },
            );

            onClick && onClick();
          }}
        >
          <YunxiaoIcon type="folder-line" />
        </Button>
      );
    } else {
      trigger = (
        <Button
          className="src-btn"
          style={{ minWidth: 0, width: 32, padding: 0, margin: 0 }}
          onClick={() => {
            track({
              action: 'view_commit_source_file',
              page: trackPage,
            });

            navigate(
              !resourcePath
                ? `/${pathResource.fullPath}/tree/${commitInfo.id}`
                : `/${pathResource.fullPath}/tree/${commitInfo.id}/${resourcePath}`,
              { state: { forceUpdate: true } },
            );

            onClick && onClick();
          }}
        >
          <YunxiaoIcon type="folder-line" />
        </Button>
      );
    }

    return (
      <Tooltip trigger={trigger} align="b">
        {intl.get({
          id: 'code-assets.components.commitIdAndSrcBtns.srcBtn.ViewSourceFiles',
          defaultMessage: '查看源文件',
        })}
      </Tooltip>
    );
  }
}
